<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "UserAccountChange".
 */
class UserAccountChange extends ActiveRecord {

    // @name 账户变动类型
    const RechargeChange = 0;
    const WithdrawChange = 1;
    const WithdrawApplyChange = 11;
    const WithdrawRefuseChange = 12;
    const EmploymentChange = 2;
    const EarnChange = 3;
    const BondChange = 4;
    const ReturnBondChange = 41;
    public static $changeSelector = [
        self::RechargeChange => ['title' => '充值', 'status' => 'blue'],
        self::WithdrawChange => ['title' => '提现', 'status' => 'red'],
        self::WithdrawApplyChange => ['title' => '申请提现', 'status' => 'red'],
        self::WithdrawRefuseChange => ['title' => '拒绝提现', 'status' => 'red'],
        self::EmploymentChange => ['title' => '雇佣费', 'status' => 'purple'],
        self::EarnChange => ['title' => '薪酬', 'status' => 'green'],
        self::BondChange => ['title' => '缴纳保证金', 'status' => 'purple'],
        self::ReturnBondChange => ['title' => '退还保证金', 'status' => 'green'],
    ];

    const StatusNormal = 0;
    public static $statusSelector = [
        self::StatusNormal => ['title' => '成功', 'status' => 'green'],
    ];

    // only define rules for those attributes that
    // will receive user inputs.
    public function rules()
    {
        return [
            [['user_id', 'type', 'amount', 'pre_change', 'after_change'], 'required'],
            [['user_id', 'type', 'amount', 'pre_change', 'after_change', 'status', 'deleted_at'], 'integer'],
        ];
    }

    /**
     * @name 字段名称
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'user_id' => '用户编号',
            'type' => '类型',
            'amount' => '操作金额',
            'pre_change' => '余额',
            'after_change' => '余额',
            'status' => '状态',
            'deleted_at' => '删除时间',
        ];
    }

    /**
     * @name 金额备注转换
     * @return string
     */
    public function getAmountText()
    {
        $text = '';
        switch($this->type) {
            case static::RechargeChange:
            case static::EarnChange:
            case static::WithdrawRefuseChange:
            case static::BondChange: {
                $text = '<span class="cl-green">+ '.$this->amount.'</span>';
            } break;
            case static::WithdrawChange:
            case static::WithdrawApplyChange:
            case static::EmploymentChange:
            case static::BondChange: {
                $text = '<span class="cl-red">- '.$this->amount.'</span>';
            } break;
            default: {
                $text = '<span class="cl-purple">? '.$this->amount.'</span>';
            }
        }
        return $text;
    }

    /**
     * @name 获取user信息
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    /**
     * @name 添加日志
     * @param $param array 数据数组
     * @return boolean
     */
    public static function changer($param)
    {
        $logger = new static();
        $logger->load([static::className() => $param], static::className());
        return $logger->save();
    }
}